package com.zhdl.modules.demo.DCHTProcess;

import com.zhdl.common.ytools.springContext.CommonParmaOpt;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.hardware_contact.hardware_web.service.HardwareContactService;
import com.zhdl.modules.demo.testprocess.ProcessOptInterface;
import com.zhdl.modules.process.BaseProcess;
import com.zhdl.modules.process.dto.ProcessBean;
import com.zhdl.modules.process.service.ProjectConfigProcessService;
import com.zhdl.modules.process.task.CustomizeTask;
import com.zhdl.modules.process.verify.DelayTimeVerify;
import com.zhdl.modules.process.verify.PlCBitValueVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.zhdl.modules.demo.DCHTProcess.CHTSmallRobotInterface.DLS_DONE;

@Slf4j
@Component
public class DCTMainProcess extends BaseProcess {
    private int slCount = 16;
    public int doneCount = 0;
    @Resource
    ProcessInitService processInitService;
    @Resource
    MainProcessInit mainProcessInit;
    @Resource
    TransportTHQProcess transportTHQProcess;
    public boolean isTransporting = false;
    @Resource
    ProjectConfigProcessService configurationPLcTest;
    @Resource
    CHTSmallRobotInterface chtSmallRobotInterface;
    @Resource
    HardwareContactService hardwareContactService;

    @Override
    public void cancelAlarm() {
        super.cancelAlarm();
        ProjectConfiguration projectConfigById = hardwareContactService.getProjectConfigById(64);
        projectConfigById.setValue("1");
        ProjectConfiguration projectConfigById1 = hardwareContactService.getProjectConfigById(33);
        projectConfigById1.setValue("1");
        ProjectConfiguration projectConfigById2 = hardwareContactService.getProjectConfigById(2);
        projectConfigById2.setValue("1");
        hardwareContactService.controlHardwareSendByProtocol(projectConfigById2, CommonParmaOpt.s7,"");
        hardwareContactService.controlHardwareSendByProtocol(projectConfigById1,CommonParmaOpt.s7,"");
        hardwareContactService.controlHardwareSendByProtocol(projectConfigById,CommonParmaOpt.s7,"");
    }

    @Override
    public void initHardware() {
        childList.add(transportTHQProcess);
        transportTHQProcess.initHardware();
        initProcessList();
        statusHolder.initComplete = true;
        statusHolder.setAuto(true);
        statusHolder.setOptInterface(new ProcessOptInterface() {
            @Override
            public void start() {
                if (isTransporting) {
                    transportTHQProcess.statusHolder.start();
                }
            }

            @Override
            public void stop() {
                transportTHQProcess.statusHolder.stop();
            }

            @Override
            public void release() {
               // transportTHQProcess.statusHolder.release();
            }

            @Override
            public void reset() {
                cancelAlarm();
            }

            @Override
            public void startProcess(Integer process) {
                 DCTMainProcess.this.startProcess(process);
            }
        });
    }

    @Override
    public void maxStepCallback(ProcessBean maxStep) {
        doneCount++;
        if (doneCount < slCount) {
            maxStep.setStep(0);
        } else {
            maxStep.setStep(-1);
        }
    }
    @Override
    public void initProcessList() {
        processBeanList = mainProcessInit.initMain(this);
    }
    public void test(Integer process) {
        statusHolder.process = process;
        statusHolder.isRunning = false;
        ProcessBean processByStep = processBeanList.get(process);
        if (processByStep != null) {
            log.info("测试流程进行至" + processByStep.getDesc() + "停止");
        }
    }
    public void startTransport(Integer process) {
        isTransporting = true;
        log.info("主流程" + process + "开启触指搬运流程");
        transportTHQProcess.statusHolder.initComplete = true;
        transportTHQProcess.startTransport(s -> {
            isTransporting = false;
            statusHolder.isRunning = false;
            statusHolder.process++;
            statusHolder.start();
        });
    }

    public void startDLS(int i) {
        ProjectConfiguration value = configurationPLcTest.getValue("打螺丝位横移轴_MoveAbs_Pos[2]");
        value.setValue((Float.parseFloat(value.value) + i * 20) + "");
        ProcessBean processBean = new ProcessBean("旋转夹紧位横移轴移动至第" + (i + 1) + "螺丝孔位置", 0, 500)
                .addDoSomeThing(() -> {
                    new MotorABSRunDo("打螺丝位横移轴", 2);
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位横移轴_MoveAbs_Done[2]"))
                .addAfterProcess(lastProcess -> startAction(new ProcessBean("SV10_打螺栓位Z轴气缸下降至打螺丝位置", 1, 500)
                        .addDoSomeThing(() -> {
                            new CylinderControlDo("SV10_打螺栓位Z轴气缸", true);
                        })
                        .addAfterProcess(lastProcess1 -> {
                            startAction(new ProcessBean("模拟大螺丝机工作过程", 1, 500)
                                    .addDoSomeThing(() -> {
                                        log.info("模拟大螺丝机工作过程....");
                                    })
                                    .addAfterProcess(lastProcess22 -> startAction(new ProcessBean("SV10_打螺栓位Z轴气缸上升至等待位", 1, 500)
                                            .addDoSomeThing(() -> {
                                                new CylinderControlDo("SV10_打螺栓位Z轴气缸", false);
                                            })
                                            .addAfterProcess(lastProcess2 -> chtSmallRobotInterface.getValue(DLS_DONE).setValue("1"))
                                            .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺栓位Z轴气缸关到位"))))
                                    .addProcessVerify(new DelayTimeVerify(2000)));
                        })
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺栓位Z轴气缸开到位"))));
        startAction(processBean);
    }

    private void startAction(ProcessBean processBean) {
        processBean.getProcessDo().doSomeThing();
        dynamicTask.startTask(new CustomizeTask(processBean), 100);
    }

}
